package com.hmyd.hmyd_android.adapter;

import java.util.List;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.InvestmentDescription;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class InvestmentDescriptionAdapter extends BaseExpandableListAdapter {

	// 分组数据
	private String[] group;
	private String[][] child;
	private Context mContext;
	private List<InvestmentDescription> mList;

	public InvestmentDescriptionAdapter(Context mContext,
			List<InvestmentDescription> list) {
		super();
		this.mContext = mContext;
		this.mList = list;

		group = new String[mList.size()];
		child = new String[mList.size()][1];

		for (int i = 0; i < mList.size(); i++) {
			group[i] = mList.get(i).label;
			child[i][0] = mList.get(i).summary;
		}
	}

	@Override
	public int getGroupCount() {
		return group.length;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return child[groupPosition].length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return group[groupPosition];
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return child[groupPosition][childPosition];
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// 实例化布局文件
		RelativeLayout glayout = (RelativeLayout) LayoutInflater.from(mContext)
				.inflate(R.layout.view_investment_description_label, null);
		ImageView iv = (ImageView) glayout.findViewById(R.id.giv);
		View line = glayout.findViewById(R.id.line);
		
		TextView tv = (TextView) glayout.findViewById(R.id.description);
		tv.setText(this.getGroup(groupPosition).toString());
		
		// 判断分组是否展开，分别传入不同的图片资源
		if (isExpanded) {
			iv.setImageResource(R.drawable.xi_icon_jin_2);
			line.setVisibility(View.GONE);
			tv.setTextColor(mContext.getResources().getColor(R.color.blue));
		} else {
			iv.setImageResource(R.drawable.xi_icon_jin);
			line.setVisibility(View.VISIBLE);
			tv.setTextColor(mContext.getResources().getColor(R.color.home_tab_text_normal));
		}
		
		if((groupPosition+1)==group.length){
			line.setVisibility(View.INVISIBLE);
		}
		
		return glayout;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// 实例化布局文件
		RelativeLayout clayout = (RelativeLayout) LayoutInflater.from(mContext)
				.inflate(R.layout.view_investment_description, null);
		TextView tv = (TextView) clayout.findViewById(R.id.description);
		tv.setText(getChild(groupPosition, childPosition).toString());
		
		View line = clayout.findViewById(R.id.line);
		
		if((groupPosition+1)==group.length){
			line.setVisibility(View.INVISIBLE);
		}
		
		return clayout;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
